<!DOCTYPE html>



  


<html class="theme-next gemini use-motion" lang="zh-Hans">
<head><meta name="generator" content="Hexo 3.8.0">
  <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform">
<meta http-equiv="Cache-Control" content="no-siteapp">
















  
  
  <link href="/limh/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css">







<link href="/limh/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css">

<link href="/limh/css/main.css?v=5.1.4" rel="stylesheet" type="text/css">


  <link rel="apple-touch-icon" sizes="180x180" href="/limh/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/limh/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/limh/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/limh/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="教程,">










<meta name="description" content="宏是程序设计的另一个基本概念，它是把一段程序代码用一个特定标识符(即：宏名)来表示。这样，在编写源程序时，程序员就可以直接使用该标识符来代替一段代码的编写，从而减少了重复代码的编写工作，也为减少错误，提高程序的可维护性提供了帮助。宏在高级语言(如：C/C++等)也有广泛的使用。 通常情况下，宏是用来代表一个具有特定功能的程序段，它只需在源程序中定义一次，但可在源程序中引用多次。只要在编写程序时需要">
<meta name="keywords" content="教程">
<meta property="og:type" content="article">
<meta property="og:title" content="9.1 宏的定义和引用">
<meta property="og:url" content="https://tdnr.gitee.io/limh/2015/04/01/asm37/index.html">
<meta property="og:site_name" content="土逗牛肉">
<meta property="og:description" content="宏是程序设计的另一个基本概念，它是把一段程序代码用一个特定标识符(即：宏名)来表示。这样，在编写源程序时，程序员就可以直接使用该标识符来代替一段代码的编写，从而减少了重复代码的编写工作，也为减少错误，提高程序的可维护性提供了帮助。宏在高级语言(如：C/C++等)也有广泛的使用。 通常情况下，宏是用来代表一个具有特定功能的程序段，它只需在源程序中定义一次，但可在源程序中引用多次。只要在编写程序时需要">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2019-01-22T07:01:50.702Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="9.1 宏的定义和引用">
<meta name="twitter:description" content="宏是程序设计的另一个基本概念，它是把一段程序代码用一个特定标识符(即：宏名)来表示。这样，在编写源程序时，程序员就可以直接使用该标识符来代替一段代码的编写，从而减少了重复代码的编写工作，也为减少错误，提高程序的可维护性提供了帮助。宏在高级语言(如：C/C++等)也有广泛的使用。 通常情况下，宏是用来代表一个具有特定功能的程序段，它只需在源程序中定义一次，但可在源程序中引用多次。只要在编写程序时需要">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/limh/',
    scheme: 'Gemini',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="https://tdnr.gitee.io/limh/2015/04/01/asm37/">





  <title>9.1 宏的定义和引用 | 土逗牛肉</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans ">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/limh/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">土逗牛肉</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/limh/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br>
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/limh/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br>
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/limh/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br>
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/limh/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br>
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/limh/about/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br>
            
            关于
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://tdnr.gitee.io/limh/limh/2015/04/01/asm37/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="土逗牛肉">
      <meta itemprop="description" content>
      <meta itemprop="image" content="/limh/images/avatar.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="土逗牛肉">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">9.1 宏的定义和引用</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2015-04-01T20:36:02+08:00">
                2015-04-01
              </time>
            

            

            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/limh/categories/汇编教程/" itemprop="url" rel="index">
                    <span itemprop="name">汇编教程</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <p>宏是程序设计的另一个基本概念，它是把一段程序代码用一个特定标识符(即：宏名)来表示。这样，在编写源程序时，程序员就可以直接使用该标识符来代替一段代码的编写，从而减少了重复代码的编写工作，也为减少错误，提高程序的可维护性提供了帮助。宏在高级语言(如：C/C++等)也有广泛的使用。</p>
<p>通常情况下，宏是用来代表一个具有特定功能的程序段，它只需在源程序中定义一次，但可在源程序中引用多次。只要在编写程序时需要，就可以直接使用它。</p>
<hr>
<h2 id="9-1-1-宏的定义"><a href="#9-1-1-宏的定义" class="headerlink" title="9.1.1 宏的定义"></a>9.1.1 宏的定义</h2><p>在使用宏之前必须先定义宏，定义宏一般格式如下：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">宏名	MACRO　[形参1, 形参2, ……]</span><br><span class="line"></span><br><span class="line">…	;宏的定义体</span><br><span class="line"></span><br><span class="line">ENDM</span><br></pre></td></tr></table></figure>
<p>在书写宏定义时，必须遵照下列规定：</p>
<ul>
<li>MACRO和ENDM是二个必须成对出现的关键字，它们分别表示宏定义的开始和结束；</li>
<li>MACRO和ENDM之间的部分是宏的定义体，它是由指令伪指令或引用其它宏所组成的程序片段，是宏所包含的具体内容；</li>
<li>“宏名”是由程序员指定的一个合法的标识符，它代表该宏；</li>
<li>宏名可以与指令助忆符伪指令名相同。在这种情况下，宏指令优先，而同名的指令或伪指令都失效；</li>
<li>在ENDM的前面不要再写一次宏名，这与段或子程序定义的结束方式有所不同；</li>
<li>在宏定义的首部可以列举若干个形式参数，每个参数之间要用逗号分隔。</li>
</ul>
<p>根据上述规定，我们提倡宏名尽可能不要与指令助忆符、伪指令相名，以免引起不必要的误会。</p>
<p><strong>例9.1 定义一个把16位数据寄存器压栈的宏。</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">PUSHR	MACRO</span><br><span class="line"></span><br><span class="line">PUSH	AX</span><br><span class="line"></span><br><span class="line">PUSH	BX</span><br><span class="line"></span><br><span class="line">PUSH	CX</span><br><span class="line"></span><br><span class="line">PUSH	DX</span><br><span class="line"></span><br><span class="line">ENDM</span><br></pre></td></tr></table></figure>
<p><strong>例9.2 定义二个字存储变量相加的宏。</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">MADDM	MACRO　OPRD1, OPRD2</span><br><span class="line"></span><br><span class="line">MOV	AX, OPRD2</span><br><span class="line"></span><br><span class="line">ADD	OPRD1, AX</span><br><span class="line"></span><br><span class="line">ENDM</span><br></pre></td></tr></table></figure>
<p>上述宏定义虽然能满足题目的要求，但由于在定义体中改变了寄存器AX的值，这就使宏的引用产生了一定的副作用。为了使寄存器AX的使用变得透明，可把该宏定义改成如下形式：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">MADDM	MACRO　OPRD1, OPRD2</span><br><span class="line"></span><br><span class="line">PUSH	AX</span><br><span class="line"></span><br><span class="line">MOV	AX, OPRD2</span><br><span class="line"></span><br><span class="line">ADD	OPRD1, AX</span><br><span class="line"></span><br><span class="line">POP	AX</span><br><span class="line"></span><br><span class="line">ENDM</span><br></pre></td></tr></table></figure>
<p>通过在宏定义的开始和结尾分别增加对所用寄存器的保护和恢复指令，就使得：对该宏的任意引用都不会产生任何副作用。</p>
<h2 id="9-1-2-宏的引用"><a href="#9-1-2-宏的引用" class="headerlink" title="9.1.2 宏的引用"></a>9.1.2 宏的引用</h2><p>在源程序中，一旦定义了某宏，那么，在该程序的任何位置都可直接引用该宏，而不必重复编写相应的程序段。引用宏的一般格式如下：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">宏名　[实参1, 实参2, ……]</span><br></pre></td></tr></table></figure>
<p>其中：实参的位置要与形参的位置要对应，但实参的个数可以与形参的个数不相等。<br>当实参的个数多于形参的个数时，多出的实参被忽略；</p>
<p>当实参的个数少于形参的个数时，没有实参对应的形参用“空”来对应。但在宏展开时，所得到的指令必须是合法的汇编指令，否则，汇编程序将会给出出错信息。</p>
<p>例如：假设已有字变量w1和w2，并且也有例9.2中的宏MADDM，那么，如果要把w2的内容加到w1中的话，就可以在源程序的代码段中按下列方式来引用该宏(点击它，可显示宏展开后的形式)：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">MADDM  w1, w2</span><br></pre></td></tr></table></figure>
<p>注意：以上的宏展开形式可以在LST文件中看到，宏展开所得到的语句前面有一个数字(如：1)，它表示宏展开的层次。</p>
<p>如果在源程序中按下列方式来引用了宏MADDM，那么，在宏展开时就会得到下列语句：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">MADDM  w1</span><br></pre></td></tr></table></figure>
<h2 id="9-1-3-宏的参数传递方式"><a href="#9-1-3-宏的参数传递方式" class="headerlink" title="9.1.3 宏的参数传递方式"></a>9.1.3 宏的参数传递方式</h2><p>在引用宏时，参数是通过“实参”替换“形参”的方式来实现传递的。参数的形式灵活多样，参数可以是常数、寄存器、存储单元和表达式，还可以是指令的操作码。</p>
<p><strong>例9.3 定义二个字存储变量相加和相减的宏。</strong></p>
<p>解：</p>
<p><strong>方法1：</strong>定义二个宏分别实现存储变量的加操作和减操作</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">MADDM	MACRO　OPRD1, OPRD2</span><br><span class="line"></span><br><span class="line">MOV　AX, OPRD2</span><br><span class="line"></span><br><span class="line">ADD　OPRD1, AX</span><br><span class="line"></span><br><span class="line">ENDM</span><br><span class="line"></span><br><span class="line">MSUBM	MACRO　OPRD1, OPRD2</span><br><span class="line"></span><br><span class="line">MOV　AX, OPRD2</span><br><span class="line"></span><br><span class="line">SUB　OPRD1, AX</span><br><span class="line"></span><br><span class="line">ENDM</span><br></pre></td></tr></table></figure>
<p><strong>方法2：</strong>定义一个宏，把存储变量的加和减操作合并在一起</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">MOPM	MACRO OP, OPRD1, OPRD2</span><br><span class="line"></span><br><span class="line">MOV　AX, OPRD2</span><br><span class="line"></span><br><span class="line">OP　OPRD1, AX</span><br><span class="line"></span><br><span class="line">ENDM</span><br></pre></td></tr></table></figure>
<p>其中：参数OP是一个对应于操作码的形式参数。</p>
<p>下面是引用宏MOPM的二个例子，它们在宏展开时将会得到下列语句：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">MOPM SUB, w1, w2 </span><br><span class="line"></span><br><span class="line">… </span><br><span class="line"></span><br><span class="line">MOPM ADD, w1, w2</span><br><span class="line">``` </span><br><span class="line"></span><br><span class="line">## 9.1.4 宏的嵌套定义</span><br><span class="line"></span><br><span class="line">宏的嵌套定义有二种方式：宏定义体内引用其它的宏和宏定义体内定义其它的宏。</span><br><span class="line"></span><br><span class="line">### 1、宏定义体内引用其它的宏</span><br><span class="line"></span><br><span class="line">在宏的定义体中又引用了其它已定义好的宏，这种宏定义方式在实际的编程过程时常会用到。如果被引用的宏还没定义的话，汇编程序将会显示出错信息。</span><br><span class="line"></span><br><span class="line">例如：</span><br></pre></td></tr></table></figure>
<p>ABS    MACRO OPRD1, OPRD2</p>
<p>…</p>
<p>MOPM SUB, OPRD1, OPRD2 ;引用前面已定义的宏MOPM</p>
<p>…</p>
<p>ENDM<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">在定义宏ABS时，引用了前面已定义好的宏MOPM。</span><br><span class="line"></span><br><span class="line">### 2、宏定义体内定义其它的宏</span><br><span class="line"></span><br><span class="line">宏的定义体内又定义了其它宏，只有在先引用了外层的宏定义，才能引用内层的宏，这是因为，当外层宏展开后，内层宏的定义才变得有效。</span><br><span class="line"></span><br><span class="line">这种定义方式虽然有其好的一面，但它使宏的定义更加隐蔽，削弱了程序的可读性，降低了程序的可维护性，因此，这种宏定义方式在实际的编程中很少使用。</span><br><span class="line"></span><br><span class="line">**例9.4 假设有三个内存字变量Oprd1，Oprd2和Oprd3，编写宏定义实现下面功能(其中：OP是除加、减之外的二元操作)：**</span><br><span class="line"></span><br><span class="line">Oprd1 ← Oprd2 OP Oprd3</span><br><span class="line"></span><br><span class="line">解：</span><br><span class="line"></span><br><span class="line">**方法1：**用宏嵌套定义的方式来实现</span><br></pre></td></tr></table></figure></p>
<p>OPMM    MACRO    NAME, OP</p>
<p>NAME    MACRO    OPRD1, OPRD2, OPRD3</p>
<p>PUSH    AX</p>
<p>MOV    AX, OPRD2</p>
<p>OP    AX, OPRD3</p>
<p>MOV    OPRD1, AX</p>
<p>POP    AX</p>
<p>ENDM</p>
<p>ENDM<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">这时，如果要完成下列操作(其中：w1，w2和w3是内存字变量)：</span><br><span class="line"></span><br><span class="line">w1 ← w2 + w3 w1 ← w2 OR w3</span><br><span class="line"></span><br><span class="line">那么，可按下列宏引用的方式来完成所需要的功能：</span><br></pre></td></tr></table></figure></p>
<p> OPMM  MADDM, ADD<br>;宏引用语句(1)：在宏扩展后将得到一个新的宏定义MADDM——把后二个内存字变量之和赋给第一个内存字变量：</p>
<p> OPMM  MORM, OR<br>;宏引用语句(2)：在宏扩展后将得到另一个新的宏定义MORM——把后二个内存字变量之“逻辑或”赋给第一个内存字变量：</p>
<p> MADDM  w1, w2, w3<br>;有了宏引用语句(1)和(2)所得到的宏定义，当前宏引用语句和下一条才有效，并在宏扩展时能得到相应的程序片段。</p>
<p>MORM   w1, w2, w3<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">**方法2：**把所要运算的指令操作符作为参数进行传递</span><br></pre></td></tr></table></figure></p>
<p>OPMM    MACRO    OP, OPRD1, OPRD2, OPRD3</p>
<p>PUSH    AX</p>
<p>MOV    AX, OPRD2</p>
<p>OP    AX, OPRD3</p>
<p>MOV    OPRD1, AX</p>
<p>POP    AX</p>
<p>ENDM<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">有了上述定义之后，可按下列宏引用的方式来完成上面的“加”和“逻辑或”的功能，单击它们可显示宏扩展时所得到的程序片段：</span><br></pre></td></tr></table></figure></p>
<p>OPMM  ADD, w1, w2, w3<br> OPMM  OR, w1, w2, w3<br><code>`</code></p>
<p>从上面二种方法的比较来看，方法2显然要简单、直观些，程序的可读性和可维护性也要高一些。所以，通常情况下，宏定义的嵌套方式在实际编程过程中是很少使用的。</p>
<h2 id="9-1-5-宏与子程序的区别"><a href="#9-1-5-宏与子程序的区别" class="headerlink" title="9.1.5 宏与子程序的区别"></a>9.1.5 宏与子程序的区别</h2><p>宏和子程序都是为了简化源程序的编写，提高程序的可维护性，但是它们二者之间存在着以下本质的区别：</p>
<ol>
<li>在源程序中，通过书写宏名来引用宏，而子程序是通过CALL指令来调用；</li>
<li>汇编程序对宏通过宏扩展来加入其定义体，宏引用多少次，就相应扩展多少次，所以，引用宏不会缩短目标程序；而子程序代码在目标程序中只出现一次，调用子程序是执行同一程序段，因此，目标程序也得到相应的简化；</li>
<li>宏引用时，参数是通过“实参”替换“形参”的方式来实现传递的，参数形式灵活多样，而子程序调用时，参数是通过寄存器、堆栈或约定存储单元进行传递的；</li>
<li>宏引用语句扩展后，目标程序中就不再有宏引用语句，运行时，不会有额外的时间开销，而子程序的调用在目标程序中仍存在，子程序的调用和返回均需要时间。</li>
</ol>
<p>总之，当程序片段不长，速度是关键因素时，可采用宏来简化源程序，但当程序片段较长，存储空间是关键因素时，可采用子程序的方法来简化源程序和目标程序。</p>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/limh/tags/教程/" rel="tag"># 教程</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/limh/2015/04/01/asm36/" rel="next" title="8.3 中断功能的分类">
                <i class="fa fa-chevron-left"></i> 8.3 中断功能的分类
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/limh/2015/04/01/asm38/" rel="prev" title="9.2 宏参数的特殊运算符">
                9.2 宏参数的特殊运算符 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  
    <div class="comments" id="comments">
      <div id="SOHUCS"></div>
    </div>

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image" src="/limh/images/avatar.jpg" alt="土逗牛肉">
            
              <p class="site-author-name" itemprop="name">土逗牛肉</p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/limh/archives/">
              
                  <span class="site-state-item-count">255</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/limh/categories/index.html">
                  <span class="site-state-item-count">8</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/limh/tags/index.html">
                  <span class="site-state-item-count">7</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          

          
            <div class="links-of-author motion-element">
                
                  <span class="links-of-author-item">
                    <a href="https://github.com/limhGeek" target="_blank" title="GitHub">
                      
                        <i class="fa fa-fw fa-github"></i>GitHub</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="mailto:1591054297@qq.com" target="_blank" title="E-Mail">
                      
                        <i class="fa fa-fw fa-envelope"></i>E-Mail</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://www.jianshu.com/u/f73b243e7cc9" target="_blank" title="简书">
                      
                        <i class="fa fa-fw fa-user-circle-o"></i>简书</a>
                  </span>
                
            </div>
          

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#9-1-1-宏的定义"><span class="nav-number">1.</span> <span class="nav-text">9.1.1 宏的定义</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#9-1-2-宏的引用"><span class="nav-number">2.</span> <span class="nav-text">9.1.2 宏的引用</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#9-1-3-宏的参数传递方式"><span class="nav-number">3.</span> <span class="nav-text">9.1.3 宏的参数传递方式</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#9-1-5-宏与子程序的区别"><span class="nav-number">4.</span> <span class="nav-text">9.1.5 宏与子程序的区别</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2019</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">土逗牛肉</span>

  
</div>


  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Gemini</a> v5.1.4</div>




        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
	var str = GetUrlParam('height');
    if (str === '0'){
		document.getElementById('header').style.display = 'none';
		document.getElementById('comments').style.display = 'none';
		document.getElementById('footer').style.display = 'none';
		document.getElementsByClassName('post-footer').item(0).style.display = 'none'
	}
	
    function GetUrlParam(paraName) {
        var url = document.location.toString();
        var arrObj = url.split("?");

        if (arrObj.length > 1) {
            var arrPara = arrObj[1].split("&");
            var arr;

            for (var i = 0; i < arrPara.length; i++) {
                 arr = arrPara[i].split("=");

                if (arr != null && arr[0] === paraName) {
                    return arr[1];
                }
            }
            return "";
        } else {
            return "";
        }
    }
</script>









  












  
  
    <script type="text/javascript" src="/limh/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/limh/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/limh/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/limh/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/limh/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/limh/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/limh/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/limh/js/src/motion.js?v=5.1.4"></script>



  
  


  <script type="text/javascript" src="/limh/js/src/affix.js?v=5.1.4"></script>

  <script type="text/javascript" src="/limh/js/src/schemes/pisces.js?v=5.1.4"></script>



  
  <script type="text/javascript" src="/limh/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/limh/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/limh/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  





  

  

  

  
  

  

  

  

</body>
</html>
